package com.liaoyifan.core.opera;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Locale;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

@SuppressWarnings("unused")
public class RequestLocaleResolver implements LocaleResolver {

    public static final String PARAM_NAME = "lang";

    @NonNull @Override
    public Locale resolveLocale(@NonNull HttpServletRequest request) {
        String lang = request.getParameter(PARAM_NAME);
        if (lang == null || lang.isBlank()) {
            return Locale.getDefault();
        }
        Locale locale = StringUtils.parseLocale(lang);
        return locale != null ? locale : Locale.getDefault();
    }

    @Override
    public void setLocale(
            @NonNull HttpServletRequest request, HttpServletResponse response, Locale locale) {}
}
